home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / fscompr / compfile.c < prev    next >
C/C++ Source or Header  |  1997-12-23  |  6KB  |  279 lines

  1. // CompFile.c: compresses a file
  2.  
  3. #include <stdio.h>
  4. #include <io.h>
  5.  
  6. #include "zlib.h"
  7.  
  8. #define BUF_SIZE    1024
  9.  
  10. z_stream ZStream;
  11. Bytef *BufIn, *BufOut;
  12. int err;
  13. FILE *fin, *fout;
  14. long length;
  15.  
  16. /*******************************************************/
  17. int LoadInput()
  18. {
  19. #if defined( _WINDOWS )
  20.     MSG msg;
  21.     while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
  22.         TranslateMessage( &msg );
  23.         DispatchMessage( &msg );
  24.     }
  25. #endif
  26.     if ( ZStream.avail_in == 0 ) {
  27.         ZStream.next_in = BufIn;
  28.         ZStream.avail_in = fread( BufIn, 1, BUF_SIZE, fin );
  29.     }
  30.     return ZStream.avail_in;
  31. }
  32.  
  33.  
  34. /*******************************************************/
  35. int FlushOutput()
  36. {
  37. unsigned int count;
  38. /*#if defined( _WINDOWS )*/
  39.     MSG msg;
  40.     
  41.     while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
  42.         TranslateMessage( &msg );
  43.         DispatchMessage( &msg );
  44.     }
  45. /*#endif*/
  46.     count = BUF_SIZE - ZStream.avail_out;
  47.     if ( count ) {
  48.         if ( fwrite( BufOut, 1, count, fout ) != count ) {
  49.             err = Z_ERRNO;
  50.             return 0;
  51.         }
  52.         ZStream.next_out = BufOut;
  53.         ZStream.avail_out = BUF_SIZE;
  54.     }
  55.     return count;
  56. }
  57.  
  58.  
  59.  
  60. /*******************************************************/
  61. int PerCent()
  62. /* returns an integer between 0 and 100 */
  63. {
  64.     if ( length == 0 )
  65.         return 100;
  66.     else if ( length > 10000000L )
  67.         return ( ZStream.total_in / ( length / 100 ) );
  68.     else
  69.         return ( ZStream.total_in * 100 / length );
  70. }
  71.  
  72.  
  73.  
  74. /*******************************************************/
  75. int EXPORT CompFile (char *OutputName, 
  76.                      char *InputName, 
  77.                      int Level, 
  78.                      long (CALLBACK *lpPercCallback)( long Perc ) )
  79. {
  80.     /* previous percent value: used to avoid calling two or more times 
  81.        the callback function with the same percent value */
  82.     int OldPerc;
  83.     int NewPerc;
  84.  
  85.     /* opens files */
  86.     fin  = fopen( InputName, "rb" );
  87.     if( fin == NULL ) return -200;
  88.     fout = fopen( OutputName, "wb" );
  89.     if( fout == NULL ) return -201;
  90.  
  91.     length = filelength( fileno( fin ) );
  92.  
  93.     /* allocates buffers */
  94.     BufIn = (Bytef*) malloc( BUF_SIZE );
  95.     if( BufIn == NULL ) {
  96.         fclose( fin );
  97.         fclose( fout );
  98.         return Z_MEM_ERROR;
  99.     }
  100.  
  101.     BufOut = (Bytef*) malloc( BUF_SIZE );
  102.     if( BufIn == NULL ) {
  103.         fclose( fin );
  104.         fclose( fout );
  105.         free( BufIn );
  106.         return Z_MEM_ERROR;
  107.     }
  108.  
  109.     ZStream.zalloc = 0;  //z_stream member
  110.     ZStream.zfree = 0;   //z_stream member
  111.     ZStream.opaque = 0;  //z_stream member
  112.  
  113.     err = Z_OK;
  114.     ZStream.avail_in = 0;
  115.     ZStream.avail_out = BUF_SIZE;
  116.     ZStream.next_out = BufOut;
  117.  
  118.     OldPerc = -1;
  119.     
  120.     err = deflateInit(&ZStream, Level);
  121.     if (err != Z_OK) return err;
  122.  
  123.     for ( ; ; ) {
  124. /*        if ( m_AbortFlag )
  125.             break;*/
  126.         if ( !LoadInput() )
  127.             break;
  128.         err = deflate( &ZStream, Z_NO_FLUSH );
  129.         FlushOutput();
  130.         if ( err != Z_OK )
  131.             break;
  132.         /*progress( percent() );*/
  133.         NewPerc = PerCent();
  134.         if( lpPercCallback != NULL && NewPerc != OldPerc ) {
  135.             (*lpPercCallback)( NewPerc );
  136.             OldPerc = NewPerc;
  137.         }
  138.     }
  139.     for ( ; ; ) {
  140. /*        if ( m_AbortFlag )
  141.             break;*/
  142.         err = deflate( &ZStream, Z_FINISH );
  143.         if ( !FlushOutput() )
  144.             break;
  145.         if ( err != Z_OK )
  146.             break;
  147.     }
  148.  
  149. /*    progress( percent() );*/
  150.         NewPerc = PerCent();
  151.         if( lpPercCallback != NULL && NewPerc != OldPerc ) {
  152.             (*lpPercCallback)( NewPerc );
  153.             OldPerc = NewPerc;
  154.         }
  155.     deflateEnd( &ZStream );
  156. /*    if ( m_AbortFlag )
  157.         status( "User Abort" );
  158.     else if ( err != Z_OK && err != Z_STREAM_END )
  159.         status( "Zlib Error" );
  160.     else {
  161.         status( "Success" );
  162.         err = Z_OK;
  163.     }*/
  164.  
  165.     if( err = Z_STREAM_END ) err = Z_OK;
  166.  
  167.     fclose( fin );
  168.     fclose( fout );
  169.  
  170.     free( BufIn );
  171.     free( BufOut );
  172.  
  173.     return err;
  174. }
  175.  
  176.  
  177.  
  178. /*******************************************************/
  179. int EXPORT DecompFile (char *OutputName, 
  180.                        char *InputName, 
  181.                        long (CALLBACK *lpPercCallback)( long Perc ) )
  182. {
  183.     /* previous percent value: used to avoid calling two or more times 
  184.        the callback function with the same percent value */
  185.     int OldPerc;
  186.     int NewPerc;
  187.  
  188.     /* opens files */
  189.     fin  = fopen( InputName, "rb" );
  190.     if( fin == NULL ) return -200;
  191.     fout = fopen( OutputName, "wb" );
  192.     if( fout == NULL ) return -201;
  193.  
  194.     length = filelength( fileno( fin ) );
  195.  
  196.     /* allocates buffers */
  197.     BufIn = (Bytef*) malloc( BUF_SIZE );
  198.     if( BufIn == NULL ) {
  199.         fclose( fin );
  200.         fclose( fout );
  201.         return Z_MEM_ERROR;
  202.     }
  203.  
  204.     BufOut = (Bytef*) malloc( BUF_SIZE );
  205.     if( BufIn == NULL ) {
  206.         fclose( fin );
  207.         fclose( fout );
  208.         free( BufIn );
  209.         return Z_MEM_ERROR;
  210.     }
  211.  
  212.     ZStream.zalloc = 0;  //z_stream member
  213.     ZStream.zfree = 0;   //z_stream member
  214.     ZStream.opaque = 0;  //z_stream member
  215.  
  216.     err = Z_OK;
  217.     ZStream.avail_in = 0;
  218.     ZStream.avail_out = BUF_SIZE;
  219.     ZStream.next_out = BufOut;
  220.  
  221.     OldPerc = -1;
  222.  
  223.     err = inflateInit(&ZStream);
  224.     if (err != Z_OK) return err;
  225.  
  226.     for ( ; ; ) {
  227. /*        if ( m_AbortFlag )
  228.             break;*/
  229.         if ( !LoadInput() )
  230.             break;
  231.         err = inflate( &ZStream, Z_NO_FLUSH );
  232.         FlushOutput();
  233.         if ( err != Z_OK )
  234.             break;
  235.         /*progress( percent() );*/
  236.         NewPerc = PerCent();
  237.         if( lpPercCallback != NULL && NewPerc != OldPerc ) {
  238.             (*lpPercCallback)( NewPerc );
  239.             OldPerc = NewPerc;
  240.         }
  241.     }
  242.     for ( ; ; ) {
  243. /*        if ( m_AbortFlag )
  244.             break;*/
  245.         err = inflate( &ZStream, Z_FINISH );
  246.         if ( !FlushOutput() )
  247.             break;
  248.         if ( err != Z_OK )
  249.             break;
  250.     }
  251.  
  252. /*    progress( percent() );*/
  253.         NewPerc = PerCent();
  254.         if( lpPercCallback != NULL && NewPerc != OldPerc ) {
  255.             (*lpPercCallback)( NewPerc );
  256.             OldPerc = NewPerc;
  257.         }
  258.     inflateEnd( &ZStream );
  259. /*    if ( m_AbortFlag )
  260.         status( "User Abort" );
  261.     else if ( err != Z_OK && err != Z_STREAM_END )
  262.         status( "Zlib Error" );
  263.     else {
  264.         status( "Success" );
  265.         err = Z_OK;
  266.     }*/
  267.  
  268.     if( err = Z_STREAM_END ) err = Z_OK;
  269.  
  270.     fclose( fin );
  271.     fclose( fout );
  272.  
  273.     free( BufIn );
  274.     free( BufOut );
  275.  
  276.     return err;
  277. }
  278.  
  279.